<?php

class Users extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'Users':
	 * @var integer $id
	 * @var string $username
	 * @var string $password
	 * @var string $true_name
	 * @var integer $sex
	 * @var string $company
	 * @var string $department
	 * @var string $position
	 * @var string $office
	 * @var string $duty
	 * @var string $entry_time
	 * @var string $mobile_no
	 * @var string $home_addr
	 * @var string $auth
	 * @var string $birthday
	 * @var string $educational_background
	 * @var string $household_register
	 * @var string $card_id
	 * @var string $home_tel
	 * @var integer $state
	 * @var string $create_time
	 */
	public $repassword;
	public $oldpassword;
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'users';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array(
			array('oldpassword','length','min'=>6,'max'=>16,'on'=>'password'),//修改密码时验证
			array('oldpassword', 'required','on'=>'password'),
			
			array('username','length','min'=>4,'max'=>16,'on'=>'create'),
			array('username', 'application.extensions.jformvalidate.ECustomJsValidator','on'=>'create',
			'rules'=> array('remote' => '?r=users/isUniqueUsername'),
			'messages'=> array('remote' => '{attribute}已被注册！'),
			),
			array('password','length','min'=>6,'max'=>16),
			array('repassword','length','min'=>6,'max'=>16),
			array('repassword','compare', 'compareAttribute' => 'password'),
			array('true_name','length','min'=>2,'max'=>8),
			array('company','length','max'=>80),
			array('department','length','max'=>80),
			array('position','length','max'=>20),
			array('office','length','max'=>80),
			array('duty','length','max'=>200),
			array('mobile_no','length','min'=>11,'max'=>12),
			array('home_addr','length','max'=>80),
			//array('auth','length','max'=>200),
			array('educational_background','length','max'=>10),
			array('household_register','length','min'=>2,'max'=>8),
			array('card_id','length','max'=>18),
			array('home_tel','length','min'=>8,'max'=>16),
			array('region','length','max'=>50),
			array('username,password,repassword,true_name,company,department,position,office,duty,educational_background,
			      birthday,entry_time,household_register,card_id,region,sex,state', 'required'),
			array('sex, state', 'numerical', 'integerOnly'=>true),
			array('birthday,entry_time', 'type','type'=>'date','message'=>'{attribute} 必须为日期型(年-月-日)'),
			array('home_tel,mobile_no', 'match' ,'pattern' => '/^[0-9 \+ -]+$/'), 
			array('card_id', 'match' ,'pattern' => '/(^\d{15}$)|(\d{17}(?:\d|x|X)$)/'),
		);
	}
	
	/**
	 * Prepares attributes before performing validation.
	 */
	protected function beforeValidate($on)//在验证之前将无需用户输入的字段写入
	{
		if($this->isNewRecord){
			$this->state=0;
			$this->create_time=date('Y-m-d G:i:s');
		}
		if(isset($_POST['Users']['auth']))//存在权限 数组转换为字符串
			$this->auth=implode(',',$this->auth);
		else
			$this->auth='';	
		return true;
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => '索引号',
			'username' => '用户名',
			'password' => '密码',
			'repassword' => '再输入密码',
			'oldpassword' => '原始密码',
			'true_name' => '姓名',
			'sex' => '性别',
			'company' => '所属企业',
			'department' => '所属部门',
			'position' => '岗位',
			'office' => '所属办事处',
			'duty' => '负责主要业务',
			'entry_time' => '入职时间',
			'mobile_no' => '手机号',
			'home_addr' => '家庭地址',
			'auth' => '权限',
			'birthday' => '出生日期',
			'educational_background' => '学历',
			'household_register' => '户籍',
			'card_id' => '身份证号',
			'home_tel' => '住宅电话',
			'region' => '所属区域',
			'state' => '状态',
			'create_time' => '创建日期',
		);
	}
	
	public function safeAttributes(){
		$params=parent::safeAttributes();
		$params[]='repassword';
		$params[]='oldpassword';
		return $params;
	}
}